#!/bin/bash

# 变量
version="v1.0.1"
WORKDIR="/devboot" # 工作目录
IMGNAME="img.img" # 镜像名字
DEVNAME="/dev/mmcblk1" # 设备地址
SIZE="+29.5G" # 一个系统分区的大小，总大小减去512M后的一半

# 工作目录
echo "请输入工作目录..."
read WORKDIR
if [ ! -e $WORKDIR ]; then
    echo "没有${WORKDIR}目录"
    exit 10
fi
cd "$WORKDIR"

# 判断镜像名字
echo "请输入镜像名字..."
read IMGNAME
if [ ! -e "$IMGNAME" ]; then
    echo "没有找到镜像"
    exit 1
fi

# 判断是否有设备
echo "请输入设备...，切记设备一定要格式化为fat32"
read DEVNAME
if [ ! -e "$DEVNAME" ]; then
    echo "没有找到设备"
    exit 2
fi

# 分配系统分区大小
device_size_bytes=$(blockdev --getsize64 $DEVNAME)
device_size_gb=$((device_size_bytes / 1073741824))
device_valid_size_gb=$(echo "$device_size_gb - 0.5" | bc)
final_result=$(echo "scale=2; $device_valid_size_gb / 2" | bc)
SIZE="+${final_result}G"

echo -e "\r\n工作目录：${WORKDIR}"
echo -e "镜像名字：${IMGNAME}"
echo -e "设备：${DEVNAME}"
echo -e "分区大小：${SIZE} \r\n"

echo -e "确认信息后输入Y继续，其它则退出..."
read CONTINUE_TIP
if [ "$CONTINUE_TIP" != "Y" ]; then
    exit 3
fi

# 判断内核是否启用配置
if grep -q '^CONFIG_MD[=ym]' /boot/config-*; then
    echo "CONFIG_MD 已启用"
else
    echo "CONFIG_MD 未启用"
    exit 4
fi

if grep -q '^CONFIG_BLK_DEV_DM[=ym]' /boot/config-*; then
    echo "CONFIG_BLK_DEV_DM 已启用"
else
    echo "CONFIG_BLK_DEV_DM 未启用"
    exit 5
fi

if grep -q '^CONFIG_BLK_DEV_LOOP[=ym]' /boot/config-*; then
    echo "CONFIG_BLK_DEV_LOOP 已启用"
else
    echo "CONFIG_BLK_DEV_LOOP 未启用"
    exit 6
fi

if grep -q '^CONFIG_DM_VERITY[=ym]' /boot/config-*; then
    echo "CONFIG_DM_VERITY 已启用"
else
    echo "CONFIG_DM_VERITY 未启用"
    exit 7
fi

echo "软件列表更新中..."
apt update
if [ $? -eq 0 ]; then
    echo "软件列表更新成功"
else
    echo "更新列表失败"
    exit 8
fi

echo "安装软件..."
apt install dosfstools kpartx rsync
if [ $? -eq 0 ]; then
    echo "安装软件成功"
else
    echo "安装软件失败"
    exit 9
fi


cd "$WORKDIR"
rm -rf boot/ osimg/ root/
mkdir boot root osimg

分区
fdisk "$DEVNAME" <<EOF
d
n
p


+512M
Y
t
c
n
p


$SIZE
n
p



w
EOF

# 刷新分区表
partprobe

# 格式化分区
echo "格式化分区..."
mkfs.fat -F 32 -n "boot" $DEVNAME"p1"
mkfs.ext4 -L rootfs1 $DEVNAME"p2"
mkfs.ext4 -L rootfs2 $DEVNAME"p3"

# 挂载boot分区
mount $DEVNAME"p1" boot/
mkdir boot/{os1,os2}

# 挂载系统分区
mount $DEVNAME"p2" root/

# 挂载镜像
losetup -f --show $IMGNAME

kpartx -va /dev/loop0
mount /dev/mapper/loop0p2 osimg/
mount /dev/mapper/loop0p1 osimg/boot/
cp -a osimg/boot/* boot/os1/
umount osimg/boot/
rsync -axv osimg/ root/
mkdir root/bootpar

sed -i "s/\(UUID=\)[[:xdigit:]-]\+\([[:blank:]]*\)\/boot/\1$(blkid -o value -s UUID ${DEVNAME}p1)\2\/bootpar/" root/etc/fstab

sed -i "s/\(UUID=\)[[:xdigit:]-]\+\([[:blank:]]*\/[[:blank:]]\+\)/\1$(blkid -o value -s UUID ${DEVNAME}p2)\2/" root/etc/fstab

echo -e "/bootpar/os1 /boot  none bind 0 0\n" >> root/etc/fstab

sed -i "s/\(UUID=\)[[:xdigit:]-]\+/\1$(blkid -o value -s UUID ${DEVNAME}p2)/" boot/os1/orangepiEnv.txt

sync && sync
umount root/
umount osimg/
kpartx -d /dev/loop0
losetup -d /dev/loop0

# 第二个系统
mount $DEVNAME"p3" root/
losetup -f --show $IMGNAME
kpartx -va /dev/loop0
mount /dev/mapper/loop0p2 osimg/
mount /dev/mapper/loop0p1 osimg/boot/
cp -a osimg/boot/* boot/os2/
umount osimg/boot/
rsync -axv osimg/ root/
mkdir root/bootpar

sed -i "s/\(UUID=\)[[:xdigit:]-]\+\([[:blank:]]*\)\/boot/\1$(blkid -o value -s UUID ${DEVNAME}p1)\2\/bootpar/" root/etc/fstab

sed -i "s/\(UUID=\)[[:xdigit:]-]\+\([[:blank:]]*\/[[:blank:]]\+\)/\1$(blkid -o value -s UUID ${DEVNAME}p3)\2/" root/etc/fstab

echo -e "/bootpar/os2 /boot  none bind 0 0\n" >> root/etc/fstab

sed -i "s/\(UUID=\)[[:xdigit:]-]\+/\1$(blkid -o value -s UUID ${DEVNAME}p3)/" boot/os2/orangepiEnv.txt

sync && sync
umount root/
umount osimg/
kpartx -d /dev/loop0
losetup -d /dev/loop0

cp -a boot/os1/* boot/
sync && sync
umount boot/
echo "制作完成，重启进入第一个系统"
